/*
  ******************************************************************************
  * @file    apt32f172_oamp.c
  * @author  APT AE Team
  * @version V1.12
  * @date    2019/03/08
  ******************************************************************************
  *THIS SOFTWARE WHICH IS FOR ILLUSTRATIVE PURPOSES ONLY WHICH PROVIDES 
  *CUSTOMER WITH CODING INFORMATION REGARDING THEIR PRODUCTS.
  *APT CHIP SHALL NOT BE HELD RESPONSIBILITY ADN LIABILITY FOR ANY DIRECT, 
  *INDIRECT DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF 
  *SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION 
  *CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.AND APT CHIP RESERVES 
  *THE RIGHT TO MAKE CHANGES IN THE SOFTWARE WITHOUT NOTIFICATION
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/ 
#include "apt32f172_oamp.h"
/* defines -------------------------------------------------------------------*/

/* externs--------------------------------------------------------------------*/

/*************************************************************/
//OPA RESET VALUE
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/  
void OPA_RESET_VALUE(void)
{
		OAMP->CR0=OPA0_CR_RST;     					/**< OPA0_CR  reset value   */
		OAMP->CR1=OPA1_CR_RST;     					/**< OPA1_CR  reset value  	*/
		OAMP->GATR0=OPA0_GATR_RST;					/**< OPA0_GATR reset value  */
		OAMP->GATR1=OPA1_GATR_RST;     				/**< OPA1_GATR reset value  */
}
/*************************************************************/
//OPA IO initial
//EntryParameter:OPA_IO_MODE_X,OPA_IO_G
//OPA_IO_MODE_X:OPA0P,OPA0N,OPA0X,OPA1P,OPA1N,OPA1X
//OPA_IO_G:OPA0P(0->PA1.4(AF7))),OPA0N(0->PA1.5(AF7))),OPA0X(0->PA1.3(AF7))),
//OPA1P(0->PA1.0(AF7))),OPA1N(0->PA1.2(AF7))),OPA1X(0->PA1.1(AF7)))
//ReturnValue:NONE
/*************************************************************/  
void OPA_IO_Init(OPA_IO_MODE_TypeDef  OPA_IO_MODE_X , U8_T OPA_IO_G )
{
	if(OPA_IO_MODE_X==OPA0P)
	{
		if(OPA_IO_G==0)
		{
			GPIOA1->CONLR=(GPIOA1->CONLR & 0XFFF0FFFF)|0x000A0000;										//OPA0P(PA1.4->AF7)
		}
	}
	else if(OPA_IO_MODE_X==OPA0N)
	{
		if(OPA_IO_G==0)
		{
			GPIOA1->CONLR=(GPIOA1->CONLR & 0XFF0FFFFF)|0x00A00000;										//OPA0N(PA1.5->AF7)
		}
	}
	else if(OPA_IO_MODE_X==OPA0X)
	{
		if(OPA_IO_G==0)
		{
			GPIOA1->CONLR=(GPIOA1->CONLR & 0XFFFF0FFF)|0x0000A000;										//OPA0X(PA1.3->AF7)
		}
	}
	else if(OPA_IO_MODE_X==OPA1P)
	{
		if(OPA_IO_G==0)
		{
			GPIOA1->CONLR=(GPIOA1->CONLR & 0XFFFFFFF0)|0x0000000A;										//OPA1P(PA1.0->AF7)
		}
	}
	else if(OPA_IO_MODE_X==OPA1N)
	{
		if(OPA_IO_G==0)
		{
			GPIOA1->CONLR=(GPIOA1->CONLR & 0XFFFFF0FF)|0x00000A00;										//OPA1N(PA1.2->AF7)
		}
	}
	else if(OPA_IO_MODE_X==OPA1X)
	{
		if(OPA_IO_G==0)
		{
			GPIOA1->CONLR=(GPIOA1->CONLR & 0XFFFFFF0F)|0x000000A0;										//OPA1X(PA1.1->AF7)
		}
	}
}
/*************************************************************/
//OPA Enable CMD
//EntryParameter:NewState,OPAx_NUM
//OPAx_NUM:OPA0_NUM,OPA1_NUM,
//
/*************************************************************/  
void OPA_EN_CMD(OPAx_selecte_TypeDef OPAx_NUM , FunctionalStatus NewState)
{
	if(OPAx_NUM==OPA0_NUM)
	{
		if (NewState != DISABLE)
		{
			OAMP->CR0|=0X01;								//Enable
		}
		else
		{
			OAMP->CR0&=0XFFFFFFFE;						//Disable
		}
	}
	else if(OPAx_NUM==OPA1_NUM)
	{
		if (NewState != DISABLE)
		{
			OAMP->CR1|=0X01;								//Enable
		}
		else
		{
			OAMP->CR1&=0XFFFFFFFE;						//Disablev
		}
	}
}
/*************************************************************/
//OPA Config Prg
//EntryParameter:OPAx_NUM,PGAEN_CMD_x,IPSEL_SET_x,PGAC_Set,GATRM_Set
//OPAx_NUM:OPA0_NUM,OPA1_NUM
//PGAEN_CMD_x:PGAEN_DISABLE,PGAEN_ENABLE
//Op_ExtPinConnect_X:Op_ExtPinConnect_EN,Op_ExtPinConnect_DIS
//IPSEL_SET_x:OPA0X(0->OPA0P,2->BGR 1.5v,3->内部模拟地);OPA1X(0->OPA1P,1->内部模拟地)
//PGAC_Set:0~7 OPA0(0->x1,1->x2,2->x3,3->x4,4->x5,5->x6,6->x7,7->x8); OPA1(0->x1,1->x10,2->x20,3->x40,4->x60,5->x80,6->x100,7->x120)
//GATRM_Set:0~15 (微调参参数,值越大放大倍速相应减小)
//ReturnValue:NONE
/*************************************************************/  
void OPA_Config_Prg(OPAx_selecte_TypeDef OPAx_NUM , PGAEN_CMD_TypeDef PGAEN_CMD_x , Op_ExtPinConnect_TypeDef Op_ExtPinConnect_X  , U8_T IPSEL_SET_x , U8_T PGAC_Set , U8_T GATRM_Set)
{
	if(OPAx_NUM==OPA0_NUM)
	{
		if(Op_ExtPinConnect_X==Op_ExtPinConnect_EN)
		{
			OAMP->GATR0=0xA77A << 16|GATRM_Set|0x08;
		}
		if(Op_ExtPinConnect_X==Op_ExtPinConnect_DIS)
		{
			OAMP->GATR0=0xA77A << 16|GATRM_Set;
		}
		OAMP->CR0=(OAMP->CR0 & 0XFFFFFCE1)|(PGAEN_CMD_x<<1)|(IPSEL_SET_x<<8)|(PGAC_Set<<2);
	}
	else if(OPAx_NUM==OPA1_NUM)
	{	
		if(Op_ExtPinConnect_X==Op_ExtPinConnect_EN)
		{
			OAMP->GATR1=0xA77A << 16|GATRM_Set|0x08;
		}
		if(Op_ExtPinConnect_X==Op_ExtPinConnect_DIS)
		{
			OAMP->GATR1=0xA77A << 16|GATRM_Set;
		}
		OAMP->CR1=(OAMP->CR1 & 0XFFFFFCE1)|(PGAEN_CMD_x<<1)|(IPSEL_SET_x<<8)|(PGAC_Set<<2);
	}
}

/******************* (C) COPYRIGHT 2018 APT Chip *****END OF FILE****/

